home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / setup / vbnet / 19 advanced win32 techniques / win32techniquesdemo / registryform.vb < prev    next >
Encoding:
Text File  |  2002-03-16  |  10.6 KB  |  258 lines

  1. Imports Microsoft.Win32
  2.  
  3. Public Class RegistryForm
  4.     Inherits System.Windows.Forms.Form
  5.  
  6. #Region " Windows Form Designer generated code "
  7.  
  8.     Public Sub New()
  9.         MyBase.New()
  10.  
  11.         'This call is required by the Windows Form Designer.
  12.         InitializeComponent()
  13.  
  14.         'Add any initialization after the InitializeComponent() call
  15.  
  16.     End Sub
  17.  
  18.     'Form overrides dispose to clean up the component list.
  19.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  20.         If disposing Then
  21.             If Not (components Is Nothing) Then
  22.                 components.Dispose()
  23.             End If
  24.         End If
  25.         MyBase.Dispose(disposing)
  26.     End Sub
  27.     Friend WithEvents Button1 As System.Windows.Forms.Button
  28.     Friend WithEvents txtOut As System.Windows.Forms.TextBox
  29.     Friend WithEvents Button2 As System.Windows.Forms.Button
  30.     Friend WithEvents Button3 As System.Windows.Forms.Button
  31.     Friend WithEvents Button4 As System.Windows.Forms.Button
  32.     Friend WithEvents Button5 As System.Windows.Forms.Button
  33.     
  34.     'Required by the Windows Form Designer
  35.     Private components As System.ComponentModel.Container
  36.  
  37.     'NOTE: The following procedure is required by the Windows Form Designer
  38.     'It can be modified using the Windows Form Designer.  
  39.     'Do not modify it using the code editor.
  40.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  41.         Me.Button1 = New System.Windows.Forms.Button()
  42.         Me.txtOut = New System.Windows.Forms.TextBox()
  43.         Me.Button2 = New System.Windows.Forms.Button()
  44.         Me.Button3 = New System.Windows.Forms.Button()
  45.         Me.Button4 = New System.Windows.Forms.Button()
  46.         Me.Button5 = New System.Windows.Forms.Button()
  47.         Me.SuspendLayout()
  48.         '
  49.         'Button1
  50.         '
  51.         Me.Button1.Location = New System.Drawing.Point(16, 16)
  52.         Me.Button1.Name = "Button1"
  53.         Me.Button1.Size = New System.Drawing.Size(128, 40)
  54.         Me.Button1.TabIndex = 0
  55.         Me.Button1.Text = "Check Word installation"
  56.         '
  57.         'txtOut
  58.         '
  59.         Me.txtOut.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
  60.                     Or System.Windows.Forms.AnchorStyles.Left) _
  61.                     Or System.Windows.Forms.AnchorStyles.Right)
  62.         Me.txtOut.Location = New System.Drawing.Point(160, 16)
  63.         Me.txtOut.Multiline = True
  64.         Me.txtOut.Name = "txtOut"
  65.         Me.txtOut.ScrollBars = System.Windows.Forms.ScrollBars.Both
  66.         Me.txtOut.Size = New System.Drawing.Size(344, 296)
  67.         Me.txtOut.TabIndex = 1
  68.         Me.txtOut.Text = ""
  69.         Me.txtOut.WordWrap = False
  70.         '
  71.         'Button2
  72.         '
  73.         Me.Button2.Location = New System.Drawing.Point(16, 64)
  74.         Me.Button2.Name = "Button2"
  75.         Me.Button2.Size = New System.Drawing.Size(128, 40)
  76.         Me.Button2.TabIndex = 0
  77.         Me.Button2.Text = "ADODB.Recordset info"
  78.         '
  79.         'Button3
  80.         '
  81.         Me.Button3.Location = New System.Drawing.Point(16, 112)
  82.         Me.Button3.Name = "Button3"
  83.         Me.Button3.Size = New System.Drawing.Size(128, 40)
  84.         Me.Button3.TabIndex = 0
  85.         Me.Button3.Text = "List COM Components"
  86.         '
  87.         'Button4
  88.         '
  89.         Me.Button4.Location = New System.Drawing.Point(16, 160)
  90.         Me.Button4.Name = "Button4"
  91.         Me.Button4.Size = New System.Drawing.Size(128, 40)
  92.         Me.Button4.TabIndex = 0
  93.         Me.Button4.Text = "Create keys and values"
  94.         '
  95.         'Button5
  96.         '
  97.         Me.Button5.Location = New System.Drawing.Point(16, 208)
  98.         Me.Button5.Name = "Button5"
  99.         Me.Button5.Size = New System.Drawing.Size(128, 40)
  100.         Me.Button5.TabIndex = 0
  101.         Me.Button5.Text = "Delete keys and values"
  102.         '
  103.         'RegistryForm
  104.         '
  105.         Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
  106.         Me.ClientSize = New System.Drawing.Size(512, 317)
  107.         Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button5, Me.Button4, Me.Button3, Me.Button2, Me.txtOut, Me.Button1})
  108.         Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  109.         Me.Name = "RegistryForm"
  110.         Me.Text = "Registry and RegistryKey"
  111.         Me.ResumeLayout(False)
  112.  
  113.     End Sub
  114.  
  115. #End Region
  116.  
  117.     ' Define the RegistryKey objects for the registry hives.
  118.     Dim regClasses As RegistryKey = Registry.ClassesRoot
  119.     Dim regCurrConfig As RegistryKey = Registry.CurrentConfig
  120.     Dim regCurrUser As RegistryKey = Registry.CurrentUser
  121.     Dim regDynData As RegistryKey = Registry.DynData
  122.     Dim regLocalMachine As RegistryKey = Registry.LocalMachine
  123.     Dim regPerfData As RegistryKey = Registry.PerformanceData
  124.     Dim regUsers As RegistryKey = Registry.Users
  125.  
  126.     Sub LogMessage(ByVal msg As String)
  127.         txtOut.AppendText(msg & ControlChars.CrLf)
  128.     End Sub
  129.  
  130.     ' Check whether Microsoft Word is installed on this computer,
  131.     ' by searching the HKEY_CLASSES_ROOT\Word.Application key.
  132.  
  133.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  134.         Dim regWord As RegistryKey = regClasses.OpenSubKey("Word.Application")
  135.         If regWord Is Nothing Then
  136.             LogMessage("Microsoft Word isn't installed")
  137.         Else
  138.             LogMessage("Microsoft Word is installed")
  139.         End If
  140.         ' Always close Registry keys after using them.
  141.         regWord.Close()
  142.     End Sub
  143.  
  144.     ' retrieve the GUID of a com component
  145.  
  146.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  147.         Dim clsid As String = GetCLSID("Adodb.Recordset")
  148.         LogMessage("CLSID of ADODB.Recordset = " & clsid)
  149.     End Sub
  150.  
  151.     ' Return the CLSID of a COM component, or "" if not found.
  152.  
  153.     Function GetCLSID(ByVal ProgId As String) As String
  154.         ' Open the key associated to the ProgID.
  155.         Dim regProgID As RegistryKey = Registry.ClassesRoot.OpenSubKey(ProgId)
  156.         If Not (regProgID Is Nothing) Then
  157.             ' If found, open the CLSID subkey.
  158.             Dim regClsid As RegistryKey = regProgID.OpenSubKey("CLSID")
  159.             If Not (regClsid Is Nothing) Then
  160.                 ' If found, get its default value. 2nd optional argument is the 
  161.                 ' string tp be returned if the specified value doesn't exist.
  162.                 ' (Returns an Object that we must convert to a string.)
  163.                 GetCLSID = CStr(regClsid.GetValue(""))
  164.                 ' Always close registry keys.
  165.                 regClsid.Close()
  166.             End If
  167.             ' Always close registry keys.
  168.             regProgID.Close()
  169.         End If
  170.     End Function
  171.  
  172.     ' display the list of all installed COM components
  173.  
  174.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
  175.         ' NOTE: on most machines the list of COM components is too large for a TextBox
  176.         '       and the output will be truncated.
  177.         txtOut.Text = ""
  178.         DisplayCOMComponents()
  179.     End Sub
  180.  
  181.     ' Print ProgID, CLSID, and path of all the COM components
  182.     ' installed on this computer.
  183.     Sub DisplayCOMComponents()
  184.         ' Open the HKEY_CLASSESROOT\CLSID key
  185.         Dim regClsid As RegistryKey = Registry.ClassesRoot.OpenSubKey("CLSID")
  186.  
  187.         ' Iterate over all the subkeys.
  188.         Dim clsid As String
  189.         For Each clsid In regClsid.GetSubKeyNames
  190.             ' Open the subkey.
  191.             Dim regClsidKey As RegistryKey = regClsid.OpenSubKey(clsid)
  192.  
  193.             ' Get the ProgID (this is the default value for this key).
  194.             Dim ProgID As String = CStr(regClsidKey.GetValue(""))
  195.             ' Get the InProcServer32 key, that holds the DLL path.
  196.             Dim regPath As RegistryKey = regClsidKey.OpenSubKey("InprocServer32")
  197.             If regPath Is Nothing Then
  198.                 ' If not found, it isn't an in-process DLL server, 
  199.                 ' let's see if it is an out-process EXE server.
  200.                 regPath = regClsidKey.OpenSubKey("LocalServer32")
  201.             End If
  202.             If Not (regPath Is Nothing) Then
  203.                 ' If either key has been found, retrieve its default value.
  204.                 Dim filePath As String = CStr(regPath.GetValue(""))
  205.                 ' Display all the relevant info gathered so far.
  206.                 LogMessage(ProgID & " " & clsid & " -> " & filePath)
  207.                 ' Always close open registry keys.
  208.                 regPath.Close()
  209.             End If
  210.             ' Always close open registry keys.
  211.             regClsidKey.Close()
  212.         Next
  213.     End Sub
  214.  
  215.     ' create new registry keys
  216.  
  217.     Dim regSoftware As RegistryKey
  218.     Dim regCompany As RegistryKey
  219.     Dim regProduct As RegistryKey
  220.  
  221.     Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
  222.         ' The following code snippets, taken together, add company/product
  223.         ' keys under the HKEY_LOCALMACHINE\SOFTWARE key, as many Windows apps do.
  224.  
  225.         ' Open the HKEY_LOCALMACHINE\SOFTWARE key.
  226.         regSoftware = Registry.LocalMachine.OpenSubKey("SOFTWARE", True)
  227.  
  228.         ' Add a key for the company name (or open it if it exists already).
  229.         regCompany = regSoftware.CreateSubKey("VB2TheMax")
  230.         ' Add another key for the product name (or open it if it exists already).
  231.         regProduct = regCompany.CreateSubKey("VBMaximizer")
  232.  
  233.         ' Create three Values under the Product key.
  234.         regProduct.SetValue("Path", "C:\VBMaximizer\Bin")   ' a string value
  235.         regProduct.SetValue("MajorVersion", 2)              ' a numeric value
  236.         regProduct.SetValue("MinorVersion", 1)              ' a numeric value
  237.  
  238.         LogMessage("The \SOFTWARE\VB2TheMax key has been created")
  239.     End Sub
  240.  
  241.     ' destroy the registry keys created previously
  242.  
  243.     Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
  244.         ' Delete the three values just added.
  245.         regProduct.DeleteValue("Path")
  246.         regProduct.DeleteValue("MajorVersion")
  247.         regProduct.DeleteValue("MinorVersion")
  248.         ' Delete the Product key, after closing it.
  249.         regProduct.Close()
  250.         regCompany.DeleteSubKey("VBMaximizer")
  251.         ' Delete the Company key, after closing it.
  252.         regCompany.Close()
  253.         regSoftware.DeleteSubKey("VB2TheMax")
  254.  
  255.         LogMessage("The \SOFTWARE\VB2TheMax key has been deleted")
  256.     End Sub
  257. End Class
  258.